﻿Public Class frmMasterItemTypeStock
    Dim dbBase As New dbMasterTable
    Dim frmCtrl As New FormControl
    Dim dsBase As DataSet
    Dim Var As New varDeclare
    Dim TypeAct As String

    Private Sub frmItemTypeStock_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.WindowState = FormWindowState.Maximized
        dsBase = dbBase.getTypeStockList()
        Textbox(False, False)
        GridAndFind1.Initialize(dsBase, "TipeStockID", ToolbarHeader1, Me, True)
        ToolbarHeader1.Iniliaze("Form : Type of Stock", Me, Nothing, GridAndFind1.getDataGridView(), "TipeStockID")
        txtItemColor.Text = "white"
    End Sub

    Private Sub Textbox(st As Boolean, clear As Boolean)
        cmbIsStock.Enabled = st
        btnChooseColor.Enabled = st
        If (st) Then
            st = False
        Else : st = True
        End If
        txtTypeStockID.ReadOnly = st
        txtTypeStockID.BackColor = Color.White
        txtName.ReadOnly = st
        txtName.BackColor = Color.White
        GridAndFind1.Enabled = st
        If (clear) Then
            txtTypeStockID.Text = ""
            txtName.Text = ""
            txtItemColor.Text = "white"
        End If
        If (TypeAct = "I") Then
            txtTypeStockID.ReadOnly = False
        Else : txtTypeStockID.ReadOnly = True
        End If
    End Sub

    Private Sub tsNew_Click(ByVal sender As Object) Handles ToolbarHeader1.tsNewClick
        TypeAct = "I"
        Textbox(True, True)
    End Sub

    Private Sub tsEdit_Click(ByVal sender As Object) Handles ToolbarHeader1.tsEditClick
        TypeAct = "U"
        Textbox(True, False)
    End Sub

    Private Sub tsDelete_Click(ByVal sender As Object) Handles ToolbarHeader1.tsDeleteClick
        If (txtTypeStockID.Text.Trim() <> "") Then
            TypeAct = "D"
            Dim result As String = dbBase.TypeStockManagement(TypeAct, txtTypeStockID.Text.Trim(), txtName.Text.Trim(), cmbIsStock.SelectedIndex.ToString(), txtItemColor.Text.Trim(), MDI.LoginID)
            If (result = "Success") Then
                frmCtrl.openChild(New frmMasterItemTypeStock, Me.ParentForm)
            Else : MessageBox.Show(result)
            End If
        Else : MessageBox.Show("Please Choose User First")
        End If
    End Sub

    Private Sub tsPrint_Click(ByVal sender As ToolbarHeader) Handles ToolbarHeader1.tsPrintClick
        ''Event 
    End Sub

    Private Sub tsSave_Click(ByVal sender As Object) Handles ToolbarHeader1.tsSaveClick
        If (txtTypeStockID.Text.Trim() <> "" And txtName.Text.Trim() <> "" And cmbIsStock.Text <> "" And txtItemColor.Text <> "") Then
            Dim result As String = dbBase.TypeStockManagement(TypeAct, txtTypeStockID.Text.Trim(), txtName.Text.Trim(), cmbIsStock.SelectedIndex.ToString(), txtItemColor.Text.Trim(), MDI.LoginID)
            If (result = "Success") Then
                frmCtrl.openChild(New frmMasterItemTypeStock, Me.ParentForm)
            Else : MessageBox.Show(result)
            End If
        Else : MessageBox.Show("Please Fill All Field")
        End If
    End Sub

    Private Sub tsRefresh_Click(ByVal sender As Object) Handles ToolbarHeader1.tsRefreshClick
        frmCtrl.openChild(New frmMasterItemTypeStock, Me.ParentForm)
    End Sub

    Private Sub tsCancel_Click(ByVal sender As Object) Handles ToolbarHeader1.tsCancelClick
        frmCtrl.openChild(New frmMasterItemTypeStock, Me.ParentForm)
    End Sub

    Private Sub GridCellEnter_Click(ByVal sender As GridAndFind, KeyValue As String) Handles GridAndFind1.GridCellEnter
        Dim dvFind As DataView = New DataView(dsBase.Tables(0))
        dvFind.RowFilter = "[TipeStockID] = '" & KeyValue & "'"
        txtTypeStockID.Text = dvFind.Item(0).Row("TipeStockID").ToString()
        txtName.Text = dvFind.Item(0).Row("Name").ToString()
        cmbIsStock.SelectedIndex = dvFind.Item(0).Row("isStock")
        txtItemColor.Text = dvFind.Item(0).Row("itemColor").ToString()
    End Sub

    Private Sub txtItemColor_TextChanged(sender As Object, e As EventArgs) Handles txtItemColor.TextChanged
        txtItemColor.BackColor = ColorTranslator.FromHtml(txtItemColor.Text.Trim())
        txtItemColor.ForeColor = ColorTranslator.FromHtml(txtItemColor.Text.Trim())
    End Sub

    Private Sub btnChooseColor_Click(sender As Object, e As EventArgs) Handles btnChooseColor.Click
        Dim cDialog As New ColorDialog()
        cDialog.Color = Label1.BackColor

        If (cDialog.ShowDialog() = DialogResult.OK) Then
            txtItemColor.Text = ColorTranslator.ToHtml(cDialog.Color)
        End If
    End Sub
End Class